import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询材料盘点
 */
export async function pageMesMaterialInventoryChecks(params) {
  const res = await request.get('/mes/mesMaterialInventoryCheck/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部材料盘点
 */
export async function listMesMaterialInventoryChecks(params) {
  const res = await request.get('/mes/mesMaterialInventoryCheck/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询材料盘点
 */
export async function getMesMaterialInventoryCheck(id) {
  const res = await request.get('/mes/mesMaterialInventoryCheck/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加材料盘点
 */
export async function addMesMaterialInventoryCheck(data) {
  const res = await request.post('/mes/mesMaterialInventoryCheck', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改材料盘点
 */
export async function updateMesMaterialInventoryCheck(data) {
  const res = await request.put('/mes/mesMaterialInventoryCheck', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除材料盘点
 */
export async function removeMesMaterialInventoryCheck(id) {
  const res = await request.delete('/mes/mesMaterialInventoryCheck/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除材料盘点
 */
export async function removeMesMaterialInventoryChecks(ids) {
  const res = await request.delete('/mes/mesMaterialInventoryCheck/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出材料盘点
 */
export async function exportMesMaterialInventoryChecks(params) {
  const res = await request({
    url: '/mes/mesMaterialInventoryCheck/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesMaterialInventoryCheck_${Date.now()}.xlsx`);
}
